package com.cgw.game.view;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.buffer.BarBuffer;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.renderer.BarChartRenderer;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;

public class SleepBarChartRenderer extends BarChartRenderer {
    private RectF mBarShadowRectBuffer = new RectF();


    public SleepBarChartRenderer(BarDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        super(chart, animator, viewPortHandler);
        this.mChart = chart;
        mHighlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mHighlightPaint.setColor(Color.rgb(0, 0, 0));
        mHighlightPaint.setAlpha(120);
        mShadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mShadowPaint.setStyle(Paint.Style.FILL);
        mBarBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBarBorderPaint.setStyle(Paint.Style.STROKE);
    }
    @Override
    protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {

        Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());

        mBarBorderPaint.setColor(dataSet.getBarBorderColor());
        mBarBorderPaint.setStrokeWidth(Utils.convertDpToPixel(dataSet.getBarBorderWidth()));

        final boolean drawBorder = dataSet.getBarBorderWidth() > 0.f;

        float phaseX = mAnimator.getPhaseX();
        float phaseY = mAnimator.getPhaseY();

        // draw the bar shadow before the values
        if (mChart.isDrawBarShadowEnabled()) {
            mShadowPaint.setColor(dataSet.getBarShadowColor());

            BarData barData = mChart.getBarData();

            final float barWidth = barData.getBarWidth();
            final float barWidthHalf = barWidth / 2.0f;
            float x;

            for (int i = 0, count = Math.min((int)(Math.ceil((float)(dataSet.getEntryCount()) * phaseX)), dataSet.getEntryCount());
                 i < count;
                 i++) {
                BarEntry e = dataSet.getEntryForIndex(i);
                x = e.getX();
                mBarShadowRectBuffer.left = x - barWidthHalf;
                mBarShadowRectBuffer.right = x + barWidthHalf;
                trans.rectValueToPixel(mBarShadowRectBuffer);
                if (!mViewPortHandler.isInBoundsLeft(mBarShadowRectBuffer.right)){
                    continue;
                }
                if (!mViewPortHandler.isInBoundsRight(mBarShadowRectBuffer.left)){
                    break;
                }
                mBarShadowRectBuffer.top = mViewPortHandler.contentTop();
                mBarShadowRectBuffer.bottom = mViewPortHandler.contentBottom();
                c.drawRect(mBarShadowRectBuffer, mShadowPaint);
            }
        }
        // initialize the buffer
        BarBuffer buffer = mBarBuffers[index];
        buffer.setPhases(phaseX, phaseY);
        buffer.setDataSet(index);
        buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));
        buffer.setBarWidth(mChart.getBarData().getBarWidth());
        buffer.feed(dataSet);
        trans.pointValuesToPixel(buffer.buffer);
        final boolean isSingleColor = dataSet.getColors().size() == 1;
        if (isSingleColor) {
            mRenderPaint.setColor(dataSet.getColor());
        }
        for (int j = 0; j < buffer.size(); j += 4) {
            if (!mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2])){
                continue;
            }
            if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j])) {
                break;
            }

            if("睡眠".equals(dataSet.getLabel())){
                if (dataSet.getEntryForIndex(j/4).getY() == 250){
                    mRenderPaint.setColor(Color.parseColor("#fca3ff"));
                }else if(dataSet.getEntryForIndex(j/4).getY() ==240){
                    mRenderPaint.setColor(Color.parseColor("#ecd4ae"));
                } else if(dataSet.getEntryForIndex(j/4).getY() ==230){
                    mRenderPaint.setColor(Color.parseColor("#aed5ec"));
                } else if(dataSet.getEntryForIndex(j/4).getY() ==225){
                    mRenderPaint.setColor(Color.parseColor("#b4aeec"));
                }else if(dataSet.getEntryForIndex(j/4).getY() ==42){
                    mRenderPaint.setColor(Color.parseColor("#A30014"));
                } else {
                    //220
                    mRenderPaint.setColor(Color.parseColor("#D4D4D4"));
                }
            }else {
                mRenderPaint.setColor(dataSet.getColor());
            }
            c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                    buffer.buffer[j + 3], mRenderPaint);

            if (drawBorder) {
                c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                        buffer.buffer[j + 3], mRenderPaint);
            }
        }
    }

    @Override
    public void drawHighlighted(Canvas c, Highlight[] indices) {
        BarData barData = mChart.getBarData();
        for (Highlight high : indices) {
            IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex());
            if (set == null || !set.isHighlightEnabled())
                continue;
            BarEntry e = set.getEntryForXValue(high.getX(), high.getY());
            /**
             * 点击其中一个柱子  获取X值  进行查询左边和右边相同高度的柱子。并进行计算整篇区域的大小
             * */

            float left = 0;
            float right = 0;
            if(e.getY()>=0.0f){
                int indexLeft = set.getEntryIndex(e);
                while (indexLeft>0&&set.getEntryForIndex(indexLeft-1).getY()==e.getY()){
                    left++;
                    indexLeft--;
                }

                int indexRight = set.getEntryIndex(e);
                while (indexRight<set.getEntryCount()-1&&set.getEntryForIndex(indexRight+1).getY()==e.getY()){
                    right++;
                    indexRight++;
                }
            }
            if (!isInBoundsX(e, set))
                continue;
            Transformer trans = mChart.getTransformer(set.getAxisDependency());
            if("睡眠".equals(set.getLabel())){
                if (e.getY() == 250){
                    mHighlightPaint.setColor(Color.parseColor("#fca3ff"));
                }else if(e.getY() ==240){
                    mHighlightPaint.setColor(Color.parseColor("#ecd4ae"));
                } else if(e.getY() ==230){
                    mHighlightPaint.setColor(Color.parseColor("#aed5ec"));
                } else if(e.getY() ==225){
                    mHighlightPaint.setColor(Color.parseColor("#b4aeec"));
                }else if(e.getY() ==42){
                    mHighlightPaint.setColor(Color.parseColor("#A30014"));
                } else {
                    //220
                    mHighlightPaint.setColor(Color.parseColor("#4E97C8"));
                }
            }else {
                mHighlightPaint.setColor(set.getHighLightColor());
            }
            float y1;float y2;
            /*boolean isStack = high.getStackIndex() >= 0 && e.isStacked();
            if (isStack) {

                if(mChart.isHighlightFullBarEnabled()) {

                    y1 = e.getPositiveSum();
                    y2 = -e.getNegativeSum();

                } else {

                    Range range = e.getRanges()[high.getStackIndex()==0?1:high.getStackIndex()];

                    y1 = range.from;
                    y2 = range.to;
                }

            } else {
                y1 = e.getY();
                y2 = 0.f;
            }*/
            y1 = e.getY();
            y2 = 0.f;
            prepareBarHighlight(e.getX(), y1, y2, barData.getBarWidth() / 2f, trans);
            if(high.getY()==0){
                continue;
            }
            setHighlightDrawPos(high, mBarRect);
            if(mBarRect.bottom - mBarRect.top ==0){
                continue;
            }
            // c.drawRect(mBarRect, mHighlightPaint);  原父类方法
            //根据mBarRect的矩形区域算出线的宽度和中心点画线
            mHighlightPaint.setStrokeCap(Paint.Cap.BUTT);
            float barWidth = mBarRect.right-mBarRect.left;
            mHighlightPaint.setPathEffect(null);
            RectF mBarRects = new RectF(mBarRect.left-barWidth*left,mBarRect.top,(mBarRect.right+barWidth*right),mBarRect.bottom);
            c.drawRect(mBarRects,mHighlightPaint);
            mHighlightPaint.setColor(set.getHighLightColor());
            c.drawLine(mBarRect.centerX()-(barWidth*right-barWidth*left)/2,mBarRect.top,mBarRect.centerX()-(barWidth*right-barWidth*left)/2,mBarRect.bottom,mHighlightPaint);
        }
    }
}
